﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using george.Helper;
using System.Web.SessionState;
using com.nm.Cache;

namespace george.Web.Helper {
	public static class ResponseHelper {
		public static void WriteLine(this System.Web.HttpResponse Response, string s) {
			Response.Write(s);
			Response.Write("\n");
		}
		public static void WriteLine(this System.Web.HttpResponse Response) {
			Response.Write("\n");
		}
		public static void WriteLine(this System.Web.HttpResponse Response, object obj, bool json = false){
			if (obj == null) {
				ResponseHelper.WriteLine(Response, "Value is NULL");
			}
			ResponseHelper.WriteLine(Response, json ? Newtonsoft.Json.JsonConvert.SerializeObject(obj) : obj.ToString());
		}

        static string GetKey(HttpSessionState session)
        {
            return $"SESSIN:{session.SessionID}";
        }

        public static HttpSessionState Set(this HttpSessionState session, string pKey, string value)
        {
            RedisCache.AddHash(GetKey(session), pKey, value, session.Timeout);
            return session;
        }
        public static HttpSessionState Set(this HttpSessionState session, string pKey, int value)
        {
            RedisCache.AddHash(GetKey(session), pKey, value, session.Timeout);
            return session;
        }
        public static HttpSessionState Set(this HttpSessionState session, string pKey, bool value)
        {
            RedisCache.AddHash(GetKey(session), pKey, value, session.Timeout);
            return session;
        }
        /// <summary>
        /// 从Session中读取字符串
        /// </summary>
        /// <param name="session"></param>
        /// <param name="pKey"></param>
        /// <param name="dv"></param>
        /// <returns></returns>
        public static string Get(this HttpSessionState session, string pKey, string dv)
        {
            var chacheKey = GetKey(session);
            if (!RedisCache.Exists(chacheKey)) return dv;
            RedisCache.Expire(chacheKey, session.Timeout);
            return RedisCache.GetHash(chacheKey, pKey, dv);
        }
        /// <summary>
        /// 从Session中读取整形值
        /// </summary>
        /// <param name="session"></param>
        /// <param name="pKey"></param>
        /// <param name="dv"></param>
        /// <returns></returns>
        public static int Get(this HttpSessionState session, string pKey, int dv)
        {
            var chacheKey = GetKey(session);
            if (!RedisCache.Exists(chacheKey)) return dv;
            RedisCache.Expire(chacheKey, session.Timeout);
            return (int)RedisCache.GetHash(chacheKey, pKey, dv);
        }
        /// <summary>
        /// 从 Session 中读取 布尔值
        /// </summary>
        /// <param name="session"></param>
        /// <param name="pKey"></param>
        /// <param name="dv"></param>
        /// <returns></returns>
        public static bool Get(this HttpSessionState session, string pKey, bool dv)
        {
            var chacheKey = GetKey(session);
            if (!RedisCache.Exists(chacheKey)) return dv;
            RedisCache.Expire(chacheKey, session.Timeout);
            return (bool)RedisCache.GetHash(chacheKey, pKey, dv);
        }
        public static HttpSessionState Remove(this HttpSessionState session, string pKey)
        {
            var chacheKey = GetKey(session);
            if (RedisCache.Exists(chacheKey))
                RedisCache.Remove(chacheKey);
            return session;
        }
    }
}
